package com.star.langchain.learn.config;

import lombok.extern.slf4j.Slf4j;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.guardrail.InputGuardrail;
import dev.langchain4j.guardrail.InputGuardrailRequest;
import dev.langchain4j.guardrail.InputGuardrailResult;

@Slf4j
public class FirstGuard implements InputGuardrail {

    public InputGuardrailResult validate(UserMessage userMessage) {
        log.info("userMessage:{}", userMessage);
        return InputGuardrailResult.success();
    }

    /**
     * Validates the input that will be sent to the LLM.
     * <p>
     * Unlike {@link #validate(UserMessage)}, this method allows to access the memory and the augmentation result (in
     * the case of a RAG).
     * <p>
     * Implementation must not attempt to write to the memory or the augmentation result.
     *
     * @param request the parameters, including the user message, the memory, and the augmentation result.
     */
    @Override
    public InputGuardrailResult validate(InputGuardrailRequest request) {
        log.info("request:{}", request);
        return validate(request.userMessage());
    }

    /**
     * Produces a successful result without any successful text
     *
     * @return The result of a successful input guardrail validation.
     */
    public InputGuardrailResult success() {
        log.info("success");
        return InputGuardrailResult.success();
    }

    /**
     * Produces a successful result with specific success text
     *
     * @param successfulText The text of the successful result.
     * @return The result of a successful input guardrail validation with a specific text.
     */
    public InputGuardrailResult successWith(String successfulText) {
        log.info("successWith:{}", successfulText);
        return InputGuardrailResult.successWith(successfulText);
    }

    /**
     * Produces a non-fatal failure
     *
     * @param message A message describing the failure.
     * @return The result of a failed input guardrail validation.
     */
    public InputGuardrailResult failure(String message) {
        log.info("failure:{}", message);
        return null;
    }

    /**
     * Produces a non-fatal failure
     *
     * @param message A message describing the failure.
     * @param cause   The exception that caused this failure.
     * @return The result of a failed input guardrail validation.
     */
    public InputGuardrailResult failure(String message, Throwable cause) {
        log.info("failure:{}", message);
        return null;
    }

    /**
     * Produces a fatal failure
     *
     * @param message A message describing the failure.
     * @return The result of a failed input guardrail validation.
     */
    @Override
    public InputGuardrailResult fatal(String message) {
        log.info("fatal:{}", message);

        return null;
    }

    /**
     * Produces a non-fatal failure
     *
     * @param message A message describing the failure.
     * @param cause   The exception that caused this failure.
     * @return The result of a failed input guardrail validation.
     */
    public InputGuardrailResult fatal(String message, Throwable cause) {
        log.info("fatal:{}", message);
        return null;
    }
}
